<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="t" uri="/WEB-INF/tour.tld"%>
<%@ page import="com.epam.entity.User" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Manager</title>
</head>
<body>
	<t:header login="${sessionScope.login}"/>
	
	<h3> User manager </h3>
	<p>
	
	<jsp:scriptlet> if (request.getAttribute("message") != null) { </jsp:scriptlet>
	<jsp:expression> request.getAttribute("message") </jsp:expression>
	<jsp:scriptlet> request.setAttribute("message", null); } </jsp:scriptlet>
	
	Change user role: 
	<p>
	<form action="controller" name="userUpdate" method="post">
		Login <input type="text" name="login"> <p>
		Role:
		<p> <input type="radio" name="role" value="ADMIN"> Admin </p>
		<p>	<input type="radio" name="role" value="USER"> User </p>	
		<input type="hidden" name="action" value="updateUser"> <p>
		<input type="submit" value="Update User"> <p>
	</form>
	
	Load user information:
	<p>
	<form action="controller">
		Login <input type="text" name=login> <p>
		<input type = "hidden" name = "action" value = "loadUser">
		<input type = "submit" value = "Load User">
		
	</form> <p>
	
	<c:if test="${requestScope.user ne null}">
	<t:user user="${requestScope.user}"/>
	</c:if>
	
	
	Load all users:
	<p>
	
	<form action="controller">
		<input type = "hidden" name = "action" value = "loadAllUsers">
		<input type = "hidden" name = "pageNumber" value = "1">
 		<input type = "submit" value = "Load All Users">
	</form> <p>	
	
	<c:if test="${requestScope.entityList ne null}">
		
		<table>
		<tr>
			<th>ID</th>
			<th>Login</th>
			<th>Role</th>
		</tr>
	
		<c:forEach items="${requestScope.entityList}" var="entity">
			<t:user user="${entity}"/>
			<p>
		</c:forEach>
		</table>
	
		
		<table border="1" cellpadding="5" cellspacing="5">
        <tr>
            <c:forEach begin="1" end="${requestScope.numberOfPages}" var="i">
             <c:choose>
                    <c:when test="${requestScope.pageNumber eq i}">
                        <td>${i}</td>
                    </c:when>
                    <c:otherwise>
                        <td><a href="controller?action=loadAllUsers&pageNumber=${i}&address=USER_MANAGER">${i}</a></td>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
        </tr>
    	</table>
    	
	
	
	<c:if test="${requestScope.pageNumber ne 1}">
			<a href="controller?action=loadAllUsers&pageNumber=${requestScope.pageNumber-1}&address=USER_MANAGER"> Previous </a>
	</c:if>
	
	<c:if test = "${requestScope.pageNumber lt requestScope.numberOfPages}">
    		<a href="controller?action=loadAllUsers&pageNumber=${requestScope.pageNumber+1}&address=USER_MANAGER"> Next </a>
    </c:if>
	
	</c:if>
		
</body>
</html>